//
//  AddCaseController.m
//  International_Hospital
//
//  Created by 张城 on 16/7/6.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "AddCaseController.h"
#import "DiseaseController.h"
#import <SVProgressHUD.h>

@interface AddCaseController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextViewDelegate>

@property (nonatomic,weak) UIImageView * imgPic;
@property (nonatomic,weak) UILabel * label2;
@property (nonatomic,weak) UILabel * label4;
@property (nonatomic,weak) UITextView * textView;

//@property (nonatomic,strong) NSMutableDictionary * imgCache;

@end

@implementation AddCaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.navigationItem.title = @"添加病例";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(doneAddCase)];
    [self settopview];
    [self setbottomView];
}


#pragma mark
#pragma mark - 提交  已经选好的信息 ..........
- (void)doneAddCase{
    
    CaseModel * model = [CaseModel new];
    NSDate * date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *time = [formatter stringFromDate:date];
    model.time = time;
    if ([self.imgPic.image isEqual:[UIImage imageNamed:@"compose_pic_add_highlighted"]]) {
        model.bol = 0;
    }else{
        model.bol = 1;
        model.image = self.imgPic.image;
        }
    if ([self.label2.text isEqual:@"请选择"]||[self.label4.text isEqual:@"请选择"]) {

        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showErrorWithStatus:@"请选择疾病类型与细分!"];
        [SVProgressHUD setMinimumDismissTimeInterval:1.5];
        return;
    }
    
    model.illnessType = self.label2.text;
    model.illnessDivide = self.label4.text;
    model.illnessDescribe = self.textView.text;
   
    if (_MyBlock) {
        self.MyBlock(model);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark
#pragma mark - 设置布局界面
- (void)settopview{
    UIView * view1 = [UIView new];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    [view1 makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(64);
        make.left.right.equalTo(0);
        make.height.equalTo(101);
    }];
    
    UILabel * label1 = [UILabel new];
    label1.text = @"疾病类型:";
    label1.font = [UIFont systemFontOfSize:15];
    [view1 addSubview:label1];
    [label1 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(20);
        make.centerY.equalTo(-25);
    }];
    
    UILabel * label3 = [UILabel new];
    label3.text = @"疾病细分:";
    label3.font = [UIFont systemFontOfSize:15];
    [view1 addSubview:label3];
    [label3 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(20);
        make.centerY.equalTo(25);
    }];
 // 疾病类型 的 选择项
    UILabel * label2 = [UILabel new];
    //  参数
    self.label2 = label2;
    label2.text = @"请选择";
    label2.font = [UIFont systemFontOfSize:15];
    label2.textColor = [UIColor grayColor];
    [view1 addSubview:label2];
    [label2 makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(-40);
        make.centerY.equalTo(-25);
    }];
    label2.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickChooseDiseaseType)];
    [label2 addGestureRecognizer:tap];
    
    
// 疾病细分的 选择项
    UILabel * label4 = [UILabel new];
    //  参数
    self.label4 = label4;
    label4.text = @"请选择";
    label4.font = [UIFont systemFontOfSize:15];
    label4.textColor = [UIColor grayColor];
    [view1 addSubview:label4];
    [label4 makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(-40);
        make.centerY.equalTo(25);
    }];
    label4.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickChooseDiseaseDivide)];
    [label4 addGestureRecognizer:tap2];
    
    UIImageView * imgv1 = [[UIImageView alloc]init];
    imgv1.image = [UIImage imageNamed:@"jt"];
    [view1 addSubview:imgv1];
    [imgv1 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(-25);
        make.right.equalTo(-20);
        make.size.equalTo(CGSizeMake(10, 15));
    }];
    
    UIImageView * imgv2 = [[UIImageView alloc]init];
    imgv2.image = [UIImage imageNamed:@"jt"];
    [view1 addSubview:imgv2];
    [imgv2 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(25);
        make.right.equalTo(-20);
        make.size.equalTo(CGSizeMake(10, 15));
    }];
    
    UIView * ziView = [[UIView alloc]init];
    ziView.backgroundColor = [UIColor grayColor];
    [view1 addSubview:ziView];
    [ziView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(20);
        make.right.equalTo(-20);
        make.center.equalTo(view1.center);
        make.height.equalTo(1);
    }];
}

#pragma mark
#pragma mark - 点击疾病类型  选择一种类型
- (void)clickChooseDiseaseType{
    
    DiseaseController * diseasec = [DiseaseController new];
    diseasec.navigationItem.title = @"疾病类型";
    diseasec.num = 1;
    diseasec.MyBlock = ^(NSString*string){
        self.label2.text = string;
    };
    [self.navigationController pushViewController:diseasec animated:YES];
}

#pragma mark
#pragma mark - 选择一种..疾病细分
- (void)clickChooseDiseaseDivide{
    
    if ([self.label2.text isEqual:@"请选择"]) {
    
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD showErrorWithStatus:@"请先选择疾病类型!"];
        [SVProgressHUD setMinimumDismissTimeInterval:1.5];
        
    }else{
    
    DiseaseController * diseasec2 = [DiseaseController new];
    diseasec2.navigationItem.title = @"疾病细分";
    diseasec2.num = 2;
    diseasec2.MyBlock = ^(NSString*string){
        self.label4.text = string;
    };
    [self.navigationController pushViewController:diseasec2 animated:YES];
    }
}

#pragma mark
#pragma mark - 设置下面view布局
- (void)setbottomView{
    UIView * bottomView = [[UIView alloc]init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    [bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(185);
        make.left.right.equalTo(0);
        make.height.equalTo(400);
    }];
    
    UILabel * bmLa1 = [UILabel new];
    bmLa1.text = @"病症描述:";
    bmLa1.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:bmLa1];
    [bmLa1 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(20);
        make.top.equalTo(10);
    }];
    
    UITextView * textView = [UITextView new];
    textView.font = [UIFont systemFontOfSize:15];
    textView.textColor = [UIColor whiteColor];
    textView.editable = YES;
    textView.delegate = self;
    self.textView = textView;
    textView.backgroundColor = [UIColor lightGrayColor];
    [bottomView addSubview:textView];
    [textView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(10);
        make.right.equalTo(-10);
        make.top.equalTo(bmLa1.bottom).offset(10);
        make.height.equalTo(150);
    }];
    
    UIView * ziView2 = [[UIView alloc]init];
    ziView2.backgroundColor = [UIColor lightGrayColor];
    [bottomView addSubview:ziView2];
    [ziView2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(20);
        make.right.equalTo(-20);
        make.center.equalTo(bottomView.center);
        make.height.equalTo(1);
    }];
    
    UILabel * bmLa2 = [UILabel new];
    bmLa2.text = @"病症照片:";
    bmLa2.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:bmLa2];
    [bmLa2 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(20);
        make.top.equalTo(ziView2.bottom).offset(20);
    }];
    
    UIImageView * imgPic = [[UIImageView alloc]init];
    imgPic.image = [UIImage imageNamed:@"compose_pic_add_highlighted"];
    imgPic.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(getPictures:)];
    [imgPic addGestureRecognizer:tap3];
    self.imgPic = imgPic;
    [bottomView addSubview:imgPic];
    [imgPic makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(50);
        make.top.equalTo(bmLa2.bottom).offset(15);
        make.size.equalTo(CGSizeMake(100, 100));
    }];
}
#pragma mark
#pragma mark - 选择照片
- (void)getPictures:(UITapGestureRecognizer*)tap3{
/*
 UIImagePickerControllerSourceTypeCamera,
 UIImagePickerControllerSourceTypeSavedPhotosAlbum
 */

    UIImagePickerController * imgpc = [UIImagePickerController new];
    imgpc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    imgpc.delegate = self;
    [self presentViewController:imgpc animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage * pictureImag = info[UIImagePickerControllerOriginalImage];
    self.imgPic.image = pictureImag;
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma mark
#pragma mark - textview 的代理方法
- (void)textViewDidEndEditing:(UITextView *)textView{
    self.textView.text = textView.text;
}

@end
